/*
 * @Author: your name
 * @Date: 2021-08-22 22:12:23
 * @LastEditTime: 2021-10-07 14:26:09
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \product-display\src\utils\util.js
 */
import commodity from '@/assets/js/commodity.js'

export function nameToData (name) {
  if (!name) return []
  const list = commodity.list
  if (name.length < 3) {
    return list[Number(name) - 11].children
  }
  let children = list[Number(name.slice(0, 2)) - 11].children
  const indexList = []
  for (let i = 2; i < name.length; i += 2) {
    indexList.push(name.slice(i, i + 2))
  }
  // // console.log(indexList, children)
  for (let index of indexList) {
    children = children.data[Number(index) - 11].children ? children.data[Number(index) - 11].children : children.data[Number(index) - 11]
  }
  return children
}

export function listToTree (list, pid) {
  let tree = []
  let temp
  for (const item of list) {
    if (item.parentId === pid) {
      item.label = item.categoryName
      item.value = item.id
      item.tabId = String(item.id)
      temp = listToTree(list, item.id)
      if (temp.length > 0) {
        item.children = temp
      }
      tree.push(item)
    }
  }
  return tree
}
